package test.nodes;

import java.util.List;
import java.util.Vector;

import bgai.ai.minimax.Board;
import bgai.ai.minimax.BoardType;
import bgai.ai.minimax.ChanceEvent;
import bgai.ai.minimax.Evaluator;
import bgai.ai.minimax.MoveEvent;

public class Node implements Board {

	private boolean leaf;
	private int value;
	private Vector<Node> childs;
	private BoardType type;
	private String name;
	
	/**
	 * Nodo intermedio
	 */
	public Node(String name, BoardType type) {
		leaf= false;
		value=0; // non rilevante
		childs= new Vector<Node>();
		this.type= type;
		this.name= name;
	}
	
	/**
	 * Nodo foglia
	 * @param value
	 */
	public Node(String name, int value, BoardType type) {
		leaf= true;
		this.value= value;
		childs= null; // non voglio figli
		this.type= type;
		this.name= name;
	}
	
	public int value() {
		if (leaf) {
			return value;
		} else {
			throw new RuntimeException("Non e' un nodo foglia");
		}
	}
	
	public void add(Node node) {
		childs.add(node);
	}
	
	@Override
	public String toString() {
		if (leaf) return "<L:"+name+">";
		else return "<I:"+name+">";
	}
	
	@Override
	public boolean isTerminal() {
		return leaf;
	}

	@Override
	public List<? extends ChanceEvent> chanceEvents() {
		Vector<Chance> ret= new Vector<Chance>();
		for (int k=0; k<childs.size(); k++) {
			ret.add(new Chance(k, childs.size()));
		}
		return ret;
	}

	@Override
	public Board successor(ChanceEvent e) {
		Chance c= (Chance) e;
		return childs.get(c.index());
	}

	@Override
	public List<? extends MoveEvent> moveEvents(Evaluator eval) {
		Vector<Move> ret= new Vector<Move>();
		for (int k=0; k<childs.size(); k++) {
			ret.add(new Move(k));
		}
		return ret;
	}

	@Override
	public Board successor(MoveEvent e) {
		Move c= (Move) e;
		return childs.get(c.index());
	}

	@Override
	public BoardType getBoardType() {
		return type;
	}

}
